package com.monster.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.monster.dao.CommentDao;
import com.monster.dao.CommentDaoImpl;
import com.monster.pojo.Comment;
import com.monster.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/textServlet1")
public class textServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Cid = request.getParameter("Cid");
        String cauthor = request.getParameter("cauthor");
        String comment = request.getParameter("comment");
        Comment comment1 = new Comment();
        User user = new User();
        user.setName(cauthor);
        comment1.setZan(Integer.parseInt(Cid));
        comment1.setUser(user);
        comment1.setContent(comment);

        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取String类型的时间
        String createdate = sdf.format(date);
        comment1.setDate(createdate);

        CommentDao commentDao = new CommentDaoImpl();
        int result = commentDao.addComment(comment1);

        //String strJSON =
        //[{"result":"","createdate":""}]
        String commentJSON = "[{\"cauthor\":\""+cauthor+"\"},{\"comment\":\""+comment+"\"},{\"result\":\""+result+"\",\"createdate\":\""+createdate+"\"}]";
        PrintWriter out = response.getWriter();
        out.print(commentJSON);
        out.flush();
        out.close();
    }
}
